package com.liuheqiao.sushegaunli.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.liuheqiao.sushegaunli.R;
import com.liuheqiao.sushegaunli.model.Notice;
import java.util.List;

public class NoticeAdapter extends RecyclerView.Adapter<NoticeAdapter.NoticeViewHolder> {
    private Context context;
    private List<Notice> notices;

    public NoticeAdapter(Context context, List<Notice> notices) {
        this.context = context;
        this.notices = notices;
    }

    @NonNull
    @Override
    public NoticeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_notice, parent, false);
        return new NoticeViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NoticeViewHolder holder, int position) {
        Notice notice = notices.get(position);
        
        holder.tvType.setText(notice.getType());
        holder.tvTitle.setText(notice.getTitle());
        holder.tvContent.setText(notice.getContent());
        holder.tvPublisher.setText(notice.getPublisher());
        holder.tvPublishTime.setText(notice.getPublishTime());

        // 设置图片列表
        if (notice.getImages() != null && !notice.getImages().isEmpty()) {
            holder.rvImages.setVisibility(View.VISIBLE);
            NoticeImageAdapter imageAdapter = new NoticeImageAdapter(context, notice.getImages());
            holder.rvImages.setAdapter(imageAdapter);
        } else {
            holder.rvImages.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return notices != null ? notices.size() : 0;
    }

    static class NoticeViewHolder extends RecyclerView.ViewHolder {
        TextView tvType;
        TextView tvTitle;
        TextView tvContent;
        TextView tvPublisher;
        TextView tvPublishTime;
        RecyclerView rvImages;

        public NoticeViewHolder(@NonNull View itemView) {
            super(itemView);
            tvType = itemView.findViewById(R.id.tv_type);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvPublisher = itemView.findViewById(R.id.tv_publisher);
            tvPublishTime = itemView.findViewById(R.id.tv_publish_time);
            rvImages = itemView.findViewById(R.id.rv_images);
            
            // 设置图片列表的布局管理器
            rvImages.setLayoutManager(new LinearLayoutManager(
                    itemView.getContext(), LinearLayoutManager.HORIZONTAL, false));
        }
    }
} 